var vehicle;
var singleVehicle;
var thisMap;
var VProxy;
var geocoder;
var vehicleIndex = 0;
var infobox=[];
var mapvariable;
var markersArray = new Array();
var pointArray = new Array();

var MapPageMediator = Objs("view.MapPageMediator",
	Mediator,
{

	initialize: function( name, viewComponent )
	{ 
		MapPageMediator.$super.initialize.call( this, name, viewComponent );
    	VProxy = this.facade.retrieveProxy( ProxyNames.VEHICLE_PROXY );
		this.registerListeners();
		thisMap=this;
                
              
		
		
	},
	registerListeners: function()
	{   var mapPage = this.get();
		
	},
	get: function()
	{
		return this.viewComponent;
	},
	unregisterListeners: function()
	{
	},
	listNotificationInterests: function()
	{
		return [
				NotificationNames.SHOW_MAP_VIEW,
				NotificationNames.SHOW_INFO_MAP_1,
				NotificationNames.SHOW_INFO_MAP_2,
                                NotificationNames.SHOW_DIR_MAP
		];
	},
    handleNotification: function( note )
	{ 
		
		switch (note.getName() )
		{ 
			case NotificationNames.SHOW_MAP_VIEW:
		
				google.maps.event.trigger(listmap, 'resize');
		        this.setMapDetails();
				break;
				
			case NotificationNames.SHOW_INFO_MAP_1:
				google.maps.event.trigger(singlemap, 'resize');
				
		        this.setSingleMapDetails();
				break;
				
			case NotificationNames.SHOW_INFO_MAP_2:
				google.maps.event.trigger(locationmap, 'resize');
		        this.setLocationMapDetails();
				break;
					
			 case NotificationNames.SHOW_DIR_MAP:
                          
				 google.maps.event.trigger(directionmap, 'resize');
			   
				 this.setDirectionMapDetails(note.body);
				 break;
		}
	},
        setDirectionMapDetails: function(location)
        {
             var request = {
                            origin: location.tlocation,
                             destination: location.vlocation,
                              travelMode: google.maps.DirectionsTravelMode.DRIVING
                          };
              directionsService.route(request, function(response, status) {
               if (status == google.maps.DirectionsStatus.OK) {
              directionsDisplay.setDirections(response);
                 }
              });
        },
	pointVehicleOnMap: function(vid,index)
	{  
		for(i=0;i<markersArray.length;i++)
		{  
			if(markersArray[i].vehicleID == vid)
			{ 
                         // google.maps.event.trigger(listmap, 'resize');
                          var bnd = new google.maps.LatLngBounds();
                            bnd.extend(markersArray[i].marker.position);
                              listmap.fitBounds(bnd);
                              listmap.setZoom(13);
                             
                              var infoHtml = '';
		
                                if(vehicle[index].v_street_address+vehicle[index].v_city+vehicle[index].v_state_name+vehicle[index].v_zip != '')
                                {
                                        infoHtml = "<div><div style='float:left;margin-left:5px;'><img  width=50 height=40 src="+vehicle[index].imageurl+"></div><div  style='float:left;margin-left:5px;'><div>"+vehicle[index].vehicle_id+"</div><div>"+vehicle[index].v_street_address+"</div><div>"+vehicle[index].v_city+"</div></div></div>"
                                }
                                else
                                {
                                        infoHtml = "<div><div style='float:left;margin-left:5px;'><img  width=50 height=40 src="+vehicle[index].imageurl+"></div><div  style='float:left;margin-left:5px;'><div>"+vehicle[index].vehicle_id+"</div><div>"+vehicle[index].street_address+"</div><div>"+vehicle[index].city+"</div></div></div>"
                                }
                                 var infoWindowOpts = {content:infoHtml};
                                var  infoWindow = new google.maps.InfoWindow(infoWindowOpts);	
		
		
                                         if(infobox.length > 0){
                                   infobox[0].close();
                                       infobox.length = 0;
                                       }
                              infoWindow.open(listmap, markersArray[i].marker);
                               infobox[0]=infoWindow
				google.maps.event.trigger(listmap, 'resize');
				//markersArray[i].marker.setMap(listmap);
			}
		}
				
	},
	showInfoPage: function(vid)
	{      
		
                if(global_user.admin_type!="Tower Administrator")
                    window.location="info_page.php?id="+vid;
                else
                    window.location="view_page.php?id="+vid;
	},
	setLocationMapDetails: function ()
	{
		google.maps.event.trigger(locationmap, 'resize');
		var address = this.getAddress();
		geocoder.geocode( { 'address': address}, function(results, status) {
			if (status == google.maps.GeocoderStatus.OK) 
			{
				var marker = new google.maps.Marker({
					position:results[0].geometry.location 
				});
				marker.setMap(locationmap);
			}
		});
	},
	getAddress:function()
	{ 
		singleVehicle = VProxy.getVehicleSingle();
		geocoder = new google.maps.Geocoder(); 
		var add;
			
		if(singleVehicle.v_street_address+singleVehicle.v_city+singleVehicle.v_state_name+singleVehicle.v_zip != '')
		{
			add = singleVehicle.v_street_address+ ',' +singleVehicle.v_city+ ',' +singleVehicle.v_state_name+ ',' +singleVehicle.v_zip;
		}
		else
		{
			add = singleVehicle.street_address+ ',' +singleVehicle.city+ ',' +singleVehicle.state_name+ ',' +singleVehicle.zip;
		}
		
		return add;
	},
	setSingleMapDetails: function ()
	{
	
		
		var address = this.getAddress();
		geocoder.geocode( { 'address': address}, function(results, status) {
			if (status == google.maps.GeocoderStatus.OK) 
			{
			thisMap.doSingleGeocoderSuccess(results[0].geometry.location,singlemap);
			
			}
		});
	},
	doSingleGeocoderSuccess:function(markerLocation, map)
	{ 
		var pointer = '';
		if(singleVehicle.current_action == singleVehicle.status){  		 
			pointer = 'images/green_pin.png';
		}else{
			pointer = 'images/red_pin.png';
		}	
		
		var marker = new google.maps.Marker({
			position:markerLocation ,
			icon:pointer,
			map:map
		});
		
		var infoHtml = '';
		
		if(singleVehicle.v_street_address+singleVehicle.v_city+singleVehicle.v_state_name+singleVehicle.v_zip != '')
		{
			infoHtml = "<div><div style='float:left;margin-left:5px;'><img  width=50 height=40 src="+singleVehicle.imageurl+"></div><div  style='float:left;margin-left:5px;'><div>"+singleVehicle.vehicle_id+"</div><div>"+singleVehicle.make_name+"</div><div>"+singleVehicle.model_name+"</div><div>"+singleVehicle.v_street_address+"</div><div>"+singleVehicle.v_city+"</div></div></div>"
		}
		else
		{
			infoHtml = "<div><div style='float:left;margin-left:5px;'><img  width=50 height=40 src="+singleVehicle.imageurl+"></div><div  style='float:left;margin-left:5px;'><div>"+singleVehicle.vehicle_id+"</div><div>"+singleVehicle.make_name+"</div><div>"+singleVehicle.model_name+"</div><div>"+singleVehicle.street_address+"</div><div>"+singleVehicle.city+"</div></div></div>"
		}
		var infoWindowOpts = {content:infoHtml};
		var infoWindow = new google.maps.InfoWindow(infoWindowOpts);
           
		google.maps.event.addListener(marker, 'click', function() { infoWindow.open(singlemap, marker);});
                 google.maps.event.addListener(singlemap, "click", function(){ infoWindow.close();});		
                
               
                
		marker.setMap(singlemap);
		google.maps.event.trigger(singlemap, 'resize');
		singlemap.setCenter(markerLocation);
		
	},
	setMapDetails: function ()
	{	
		vehicleIndex = 0;
		mapvariable=0;
		vehicle = VProxy.getVehicleTable();
		if (markersArray.length>0) {
			for (i in markersArray) {
			  markersArray[i].marker.setMap(null);
			}
		}
             
		markersArray = [];
		for(i=0;i<vehicle.length;i++)
		{
			google.maps.event.trigger(listmap, 'resize');
			geocoder = new google.maps.Geocoder(); 
		
			var address;
			
			if(vehicle[i].v_street_address+vehicle[i].v_city+vehicle[i].v_state_name+vehicle[i].v_zip != '')
			{
				address = vehicle[i].v_street_address+ ',' +vehicle[i].v_city+ ',' +vehicle[i].v_state_name+ ',' +vehicle[i].v_zip;
			}
			else
			{
				address = vehicle[i].street_address+ ',' +vehicle[i].city+ ',' +vehicle[i].state_name+ ',' +vehicle[i].zip;
			}
			
			geocoder.geocode( { 'address': address}, function(results, status) {

				if (status == google.maps.GeocoderStatus.OK) {
					
					pointArray.push(results[0].geometry.location);

					thisMap.doGeocoderSuccess(results[0].geometry.location);
					            
				}else{
					vehicleIndex++;	
					mapvariable+=1;
				}
		
			});
			
			
			
		}

		var innerHtmlCode= '<table class="table"><tbody>';
                var innerHtmlCode1= '<table class="table"><tbody>';    
		
		
		for(i=0;i<vehicle.length;i++){
			if(vehicle[i].current_action == vehicle[i].status)
			{
				innerHtmlCode += '<tr onClick=thisMap.pointVehicleOnMap('+ vehicle[i].vehicle_id +','+i+')><td style="font-weight:bold;font-size:12px;"><a href="javascript:thisMap.showInfoPage('+ vehicle[i].vehicle_id +')">'+ vehicle[i].year +' '+ vehicle[i].make_name +' '+ vehicle[i].model_name +'</a><div class="muted" style="font-weight:normal"><small>#'+ vehicle[i].vehicle_id +'</small></div></td><td style="text-align:right;font-size:12px;">'+ vehicle[i].v_city +'<div class="muted"></div></td></tr>';
			}
			else
			{
				innerHtmlCode1 += '<tr onClick=thisMap.pointVehicleOnMap('+ vehicle[i].vehicle_id +','+i+')><td style="font-weight:bold;font-size:12px;"><a href="javascript:thisMap.showInfoPage('+ vehicle[i].vehicle_id +')">'+ vehicle[i].year +' '+ vehicle[i].make_name +' '+ vehicle[i].model_name +'</a><div class="muted" style="font-weight:normal"><small>#'+ vehicle[i].vehicle_id +'</small></div></td><td style="text-align:right;font-size:12px;">'+ vehicle[i].v_city +'<div class="muted"><small></small></div></td></tr>';
			}
			
		}
                innerHtmlCode +='<tbody></table>';
                innerHtmlCode1 +='<tbody></table>';
                document.getElementById("non-replies-list").innerHTML = innerHtmlCode;
		document.getElementById("replies-list").innerHTML = innerHtmlCode1;
                
	},
	centerMap:function()
	{

		var bnd = new google.maps.LatLngBounds();
		for(var k=0;k<arr.length;k++){
		
			bnd.extend(pointArray[k]);
		}
		
		
	},
	doGeocoderSuccess:function(markerLocation)
	{     
		var pointer = '';
		if(vehicle[vehicleIndex].current_action == vehicle[vehicleIndex].status){  		 
			pointer = 'images/green_pin.png';
		}else{
			pointer = 'images/red_pin.png';
		}	
		
		var marker = new google.maps.Marker({
			position:markerLocation ,
			icon:pointer,
			map:listmap
		});
		
		var infoHtml = '';
		
		if(vehicle[vehicleIndex].v_street_address+vehicle[vehicleIndex].v_city+vehicle[vehicleIndex].v_state_name+vehicle[vehicleIndex].v_zip != '')
		{
			infoHtml = "<div><div style='float:left;margin-left:5px;'><img  width=50 height=40 src="+vehicle[vehicleIndex].imageurl+"></div><div  style='float:left;margin-left:5px;'><div>"+vehicle[vehicleIndex].vehicle_id+"</div><div>#"+vehicle[vehicleIndex].make_name+"</div><div>"+vehicle[vehicleIndex].model_name+"</div><div>"+vehicle[vehicleIndex].v_street_address+"</div><div>"+vehicle[vehicleIndex].v_city+"</div></div></div>"
		}
		else
		{
			infoHtml = "<div><div style='float:left;margin-left:5px;'><img  width=50 height=40 src="+vehicle[vehicleIndex].imageurl+"></div><div  style='float:left;margin-left:5px;'><div>"+vehicle[vehicleIndex].vehicle_id+"</div><div>#"+vehicle[vehicleIndex].make_name+"</div><div>"+vehicle[vehicleIndex].model_name+"</div><div>"+vehicle[vehicleIndex].street_address+"</div><div>"+vehicle[vehicleIndex].city+"</div></div></div>"
		}
                 var infoWindowOpts = {content:infoHtml};
		var  infoWindow = new google.maps.InfoWindow(infoWindowOpts);	
                
		google.maps.event.addListener(marker, 'click', function() {
                  
                     if(infobox.length > 0){
                 infobox[0].close();
                 infobox.length = 0;
            }
                 
				  infoWindow.open(listmap, marker);
				  infobox[0]=infoWindow
				 
		});
                
                google.maps.event.addListener(listmap, "click", function(){ infoWindow.close();});
                
		marker.setMap(listmap); 
		var markerObj = new Object();
		markerObj.marker = marker;
		markerObj.vehicleID = vehicle[vehicleIndex].vehicle_id;
		markersArray.push(markerObj);	
		vehicleIndex++;
		mapvariable += 1 ;
	}

});

